package modelLayer;

/**
 * This class is a modelLayer class of the MSAS system. It holds
 * information on a single teachingblock. True to our domain model, the teachingblock
 * class has field visibility to Class, ClassRoom and Subject. The teachingblock itself
 * is referenced by Block. 
 * 
 * @author Danny M�lgaard, Anna Kelemen, Jakob Korsgaard
 * 		   
 * @version 1.3.1
 * 					1.3.1: 		Added more extensive documentation. Verified that
 * 								information contained in the Design Class Diagram,
 * 								is in accordance with the information contained in this
 * 								class.
 * 					1.3.2:		toString() adjusted
 */
public class TeachingBlock {
	
	private int id;
	private String name;
	private ClassRoom cr;
	private Class c;
	private Subject s;
	private Teacher t;

	
	/**
	 * Instantiating a TeachingBlock object, taking the following parameters: 
	 * 
	 * @param 	c 	Class		the class object to be referenced in the teachingblock.
	 * @param 	s 	Subject		the Subject object to be referenced in the teachingblock.
	 * @param 	cr 	ClassRoom	the ClassRoom object to be referenced in the teachingblock.
	 * @param	t	Teacher		the Teacher object to be referenced in the teachingblock.
	 */
	public TeachingBlock(Class c, Subject s, ClassRoom cr, Teacher t) {
		
		this.setClassRoom(cr);
		this.setCl(c);
		this.setSubject(s);
		this.setTeacher(t);
		generateName();
		
	}
	
	/**
	 * Instantiating a TeachingBlock object, taking the following parameters: 
	 * 
	 * @param 	c 	Class		the class object to be referenced in the teachingblock.
	 * @param 	s 	Subject		the Subject object to be referenced in the teachingblock.
	 * @param 	cr 	ClassRoom	the ClassRoom object to be referenced in the teachingblock.
	 */
	public TeachingBlock(Class c, Subject s, ClassRoom cr) {
		
		this.setClassRoom(cr);
		this.setCl(c);
		this.setSubject(s);
		generateName();
		
	}

	/**
	 * Helper method to generate name for the teachingblock in order to be able showing it in the GUI. 
	 */
	private void generateName(){
		String grade = c != null ? c.getGrade() : "?";
		String subjectAbbreviation = s != null && s.getName() != null ? s.getName().substring(0, 3) : "?";
		String classRoom = cr != null ? cr.getName() : "?";
		this.setName(grade + subjectAbbreviation + classRoom);
	}

	/**
	 * Instantiating a TeachingBlock object, taking the following parameters: 
	 * 
	 * @param 	id 	int		the id of the teachingblock as allocated in the database.
	 */
	public TeachingBlock(int id) {
		
		this.setId(id);
		
	}
	
	/**
	 * Instantiating a TeachingBlock object, taking no parameters: 
	 */
	public TeachingBlock() {	
	}

	/**
	 * Assigns a parameter to the cr field, referencing a ClassRoom object.
	 * 
	 * @param 	cr		ClassRoom	the ClassRoom object to be referenced by
	 * 								the teachingblock.					
	 */
	public void setClassRoom(ClassRoom cr) {
		
		this.cr = cr;
		generateName();
	
	}
	
	/**
	 * Assigns a parameter to the c field, referencing a Class object.
	 * 
	 * @param 	c		Class		the Class object to be referenced by
	 * 								the teachingblock.					
	 */
	public void setCl(Class c) {
	
		this.c = c;
		generateName();
	}
	
	/**
	 * Assigns a parameter to the s field, referencing a Subject object.
	 * 
	 * @param 	s		Subject		the Subject object to be referenced by
	 * 								the teachingblock.					
	 */
	public void setSubject(Subject s) {
	
		this.s = s;
		generateName();
	}

	/**
	 * Assigns a parameter to the t field, referencing a Teacher object.
	 * 
	 * @param 	t		Teacher		the Teacher object to be referenced by
	 * 								the teachingblock.					
	 */
	public void setTeacher(Teacher t) {
	
		this.t = t;
		generateName();
		
	}
	
	
	/**
	 * Assigns a parameter to the id field, defining the, by the database allocated, id
	 * of the teachingblock.
	 * 
	 * @param 	id			int		the id the teachingblock in the database.					
	 */
	public void setId(int id) {
		
		this.id = id;
		
	}
	
	/**
	 * Assigns a parameter to the name field.
	 * 
	 * @param 	name		String	the name of the teachingblock.					
	 */
	public void setName(String name) {

		this.name = name;
		
	}
	
	/**
	 * Returns the ClassRoom object associated with the TeachingBlock object.
	 * 
	 * @return 	cr			ClassRoom	the ClassRoom object referenced by the TeachingBlock object
	 */
	public ClassRoom getClassRoom() {
		
		return this.cr;
	
	}
	
	/**
	 * Returns the Class object associated with the TeachingBlock object.
	 * 
	 * @return 	c			Class		the Class object referenced by the TeachingBlock object
	 */
	public Class getCl() {
	
		return this.c;
	
	}

	/**
	 * Returns the Subject object associated with the TeachingBlock object.
	 * 
	 * @return 	s			Subject		the Subject object referenced by the TeachingBlock object
	 */
	public Subject getSubject() {
	
		return this.s;
	
	}
		
	/**
	 * Returns teachingblock's id, as allocated in the database, to the caller.
	 * 
	 * @return	id	int		id of the teachingblock.
	 */
	public int getId() {
		
		return this.id;
		
	}
	
	/**
	 * Returns the teachingblock's name  to the caller.
	 * 
	 * @return	name	String		name of the teachingblock.
	 */
	public String getName() {
	
		return this.name;

	}
	
	/**
	 * Returns the Teacher object associated with the TeachingBlock object.
	 * 
	 * @return 	t			Teacher		the Teacher object referenced by the TeachingBlock object
	 */
	public Teacher getTeacher() {
		
		return this.t;
		
	}

	/**
	 * Overrides the object's default toString() method. Returns the object as a String
	 * literal to the caller.
	 * 
	 * return				String	the TeachingBlock object represented as a String object.
	 */
	@Override
	public String toString() {

//		return "TeachingBlock (" + this.name + "):\n " +
//			   "Class Name: " + this.c.getClassName() + "\n" +
//			   "Subject: " + this.s.getName() + "\n " +
//			   "Classroom: " + this.cr.getName() + "\n";
		return this.name;
	}

	/**
	 * Implements a check, to verify if an object is the same
	 * as this object. Uses student identification number to
	 * verify.
	 * 
	 * @param	o		Object	any object type.
	 * @return	true			if: 
	 * 							object is not null, and
	 * 							object is of type TeachingBlock, and
	 * 							object id field matches this objects id field.
	 * @return	false			if:
	 * 							object is null, or
	 * 							object is not an instance of TeachingBlock, or
	 * 							object id field doesn't match this objects id field.
	 */
	@Override
	public boolean equals(Object o) {
		
		if (o != null && (o instanceof TeachingBlock)){
			TeachingBlock tb = (TeachingBlock) o;
			if (this.id == tb.getId()) {
				return true;
			}
		}
			
		return false;

	}	
		
}
